home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 04 class fundamentals / objectpool / classes.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  2.3 KB  |  75 lines

  1. ' a class that contains an array of 100,000 random elements
  2.  
  3. Class RandomArray
  4.     Public ReadOnly arrRand(100000) As Double
  5.  
  6.     ' the constructor creates the random array
  7.     Sub New()
  8.         Dim i As Integer
  9.         Dim rand As New Random()
  10.         ' a time consuming operation
  11.         For i = 0 To UBound(arrRand)
  12.             arrRand(i) = rand.NextDouble
  13.         Next
  14.     End Sub
  15. End Class
  16.  
  17. ' this is the version of RandomArray that is ready to be pooled
  18.  
  19. Class RandomArrayPooled
  20.     Inherits RandomArray
  21.  
  22.     ' A reference to the pool manager for this class
  23.     Public OwnerPoolManager As PoolManager
  24.  
  25.     Protected Overrides Sub Finalize()
  26.         If OwnerPoolManager Is Nothing Then
  27.             ' This instance isn't owned by a pool manager
  28.  
  29.         ElseIf OwnerPoolManager.PooledObjects Is Nothing Then
  30.             ' This instance has an owner pool manager, but it was already finalized
  31.  
  32.         Else
  33.             ' when this object is finalized, put it back in the pooledobjects stack
  34.             OwnerPoolManager.PooledObjects.Push(Me)
  35.             ' re-register this object for the Finalize method
  36.             GC.ReRegisterForFinalize(Me)
  37.         End If
  38.     End Sub
  39. End Class
  40.  
  41. ' a class that pools one or more RandomArray objects
  42.  
  43. Class PoolManager
  44.     Implements IDisposable
  45.  
  46.     ' this stack contains all the objects in the pool
  47.     Public PooledObjects As New Stack()
  48.  
  49.     ' return a new instance of the RandomArray class
  50.     Function NewRandomArray() As RandomArrayPooled
  51.         If PooledObjects.Count > 1 Then
  52.             ' if there is an object in the pool, use it
  53.             Return DirectCast(PooledObjects.Pop, RandomArrayPooled)
  54.         Else
  55.             ' otherwise create a new object
  56.             NewRandomArray = New RandomArrayPooled()
  57.             NewRandomArray.OwnerPoolManager = Me
  58.         End If
  59.     End Function
  60.  
  61.     Sub Dispose() Implements IDisposable.Dispose
  62.         ' first, suppress Finalize for all pooled objects
  63.         Dim ra As RandomArray
  64.  
  65.         For Each ra In PooledObjects
  66.             DirectCast(ra, RandomArrayPooled).OwnerPoolManager = Nothing
  67.             GC.SuppressFinalize(ra)
  68.         Next
  69.  
  70.         ' inform other objects not to hook this parent pool manager again
  71.         PooledObjects = Nothing
  72.     End Sub
  73.  
  74. End Class
  75.